﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmSupplier_Editor : XtraForm
    {
        public FrmSupplier_Editor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 主键值，非正零为新增
        /// </summary>
        public int ID;
        /// <summary>
        /// 区域编号
        /// </summary>
        private string AreaCode;
        /// <summary>
        /// 区域ID
        /// </summary>
        private int AreaID;
        /// <summary>
        /// 标题栏文字格式
        /// </summary>
        private string captionFormat;

        private void FrmClient_Editor_Load(object sender, EventArgs e)
        {
            captionFormat = Text;
        }

        private void btnFirst_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }

        private void btnPrevious_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }

        private void btnNext_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }

        private void btnLast_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewRecord();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            btnAreaCode.Leave -= new EventHandler(btnAreaCode_Leave);
            txtCode.Focus();
            btnAreaCode.Leave += new EventHandler(btnAreaCode_Leave);

            if (!(ValidateArea() && ValidateInput()))
            {
                return;
            }
            if (ID <= 0)
            {
                SavaNew();
            }
            else
            {
                SaveModify();
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                if (DialogResult.Yes == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    var bll = new DMZS.BLL.Supplier();
                    bll.Delete(ID);
                    NewRecord();
                }
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData();
            }
        }

        private void SaveModify()
        {
            var bll = new DMZS.BLL.Supplier();
            var model = new DMZS.Model.Supplier();
            model.ID = ID;
            model.AreaID = AreaID;
            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.NamePY = CommonControl.StringHelper.GetFirstLetterOfChinese(model.Name, true);
            model.FullName = txtFullName.Text.Trim();
            model.Address = txtAddress.Text.Trim();
            model.IsStop = Convert.ToInt32(chkIsStop.Checked);
            model.Linkman = txtLinkman.Text.Trim();
            model.LinkPhone = txtLinkPhone.Text.Trim();
            model.Linkman1 = txtLinkman1.Text.Trim();
            model.LinkPhone1 = txtLinkPhone1.Text.Trim();
            model.PostCode = txtPostCode.Text.Trim();
            model.Fax = txtFax.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            model.Remark = mmoRemark.Text;

            var result = bll.UpdateExpand(model);
            if (result > 0)
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                ID = result;
                LoadData();
            }
            else
            {
                if (result == -1001)
                {
                    XtraMessageBox.Show("编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }
                else
                {
                    if (result == -1002)
                    {
                        XtraMessageBox.Show("该产品类型不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnAreaCode.Focus();
                        btnAreaCode.SelectAll();
                    }
                    else
                    {
                        if (result == 0)
                        {
                            XtraMessageBox.Show("该条数据已被删除，请刷新数据。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                        else
                        {
                            XtraMessageBox.Show("保存失败。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }

        private void SavaNew()
        {
            var bll = new DMZS.BLL.Supplier();
            var model = new DMZS.Model.Supplier();
            model.ID = ID;
            model.AreaID = AreaID;
            model.Code = txtCode.Text.Trim();
            model.Name = txtName.Text.Trim();
            model.NamePY = CommonControl.StringHelper.GetFirstLetterOfChinese(model.Name, true);
            model.FullName = txtFullName.Text.Trim();
            model.Address = txtAddress.Text.Trim();
            model.IsStop = Convert.ToInt32(chkIsStop.Checked);
            model.Linkman = txtLinkman.Text.Trim();
            model.LinkPhone = txtLinkPhone.Text.Trim();
            model.Linkman1 = txtLinkman1.Text.Trim();
            model.LinkPhone1 = txtLinkPhone1.Text.Trim();
            model.PostCode = txtPostCode.Text.Trim();
            model.Fax = txtFax.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            model.Remark = mmoRemark.Text;

            var result = bll.AddExpand(model);
            if (result > 0)
            {
                XtraMessageBox.Show("保存成功。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                ID = result;
                LoadData();
            }
            else
            {
                if (result == -1001)
                {
                    XtraMessageBox.Show("编号重复。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtCode.Focus();
                    txtCode.SelectAll();
                }
                else
                {
                    if (result == -1002)
                    {
                        XtraMessageBox.Show("该产品类型不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        btnAreaCode.Focus();
                        btnAreaCode.SelectAll();
                    }
                    else
                    {
                        XtraMessageBox.Show("保存失败。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void FrmClient_Editor_Shown(object sender, EventArgs e)
        {
            if (ID <= 0)
            {
                NewRecord();
            }
            else
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            Text = string.Format(captionFormat, "维护");

            var bll = new DMZS.BLL.Supplier();
            var row = bll.GetRecordByID(ID);

            if (row == null)
            {
                XtraMessageBox.Show("该条数据已经被删除，请刷新数据", Text, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                Close();
                return;
            }

            AreaID = Convert.ToInt32(row["AreaID"]);
            AreaCode = row["AreaCode"].ToString();
            btnAreaCode.Text = row["AreaCode"].ToString();
            btnAreaCode.Properties.ReadOnly = true;
            btnAreaCode.Properties.Buttons[0].Enabled = false;
            btnAreaCode.TabStop = false;
            txtAreaName.Text = row["AreaName"].ToString();
            txtCode.Text = row["Code"].ToString();
            txtCode.Properties.ReadOnly = true;
            txtCode.TabStop = false;
            txtName.Text = row["Name"].ToString();
            txtFullName.Text = row["FullName"].ToString();
            txtAddress.Text = row["Address"].ToString();
            chkIsStop.Checked = Convert.ToBoolean(row["IsStop"]);

            txtLinkman.Text = row["Linkman"].ToString();
            txtLinkPhone.Text = row["LinkPhone"].ToString();
            txtLinkman1.Text = row["Linkman1"].ToString();
            txtLinkPhone1.Text = row["LinkPhone1"].ToString();
            txtPostCode.Text = row["PostCode"].ToString();
            txtFax.Text = row["Fax"].ToString();
            txtEmail.Text = row["Email"].ToString();
            mmoRemark.Text = row["Remark"].ToString();

            txtName.Focus();
        }

        private void NewRecord()
        {
            Text = string.Format(captionFormat, "新增");
            ID = 0;

            AreaID = 0;
            AreaCode = string.Empty;
            btnAreaCode.Text = string.Empty;
            btnAreaCode.Properties.ReadOnly = false;
            btnAreaCode.Properties.Buttons[0].Enabled = true;
            btnAreaCode.TabStop = true;
            txtAreaName.Text = string.Empty;
            txtCode.Text = string.Empty;
            txtCode.Properties.ReadOnly = false;
            txtCode.TabStop = true;
            txtName.Text = string.Empty;
            txtFullName.Text = string.Empty;
            txtAddress.Text = string.Empty;
            chkIsStop.Checked = false;

            txtLinkman.Text = string.Empty;
            txtLinkPhone.Text = string.Empty;
            txtLinkman1.Text = string.Empty;
            txtLinkPhone1.Text = string.Empty;
            txtPostCode.Text = string.Empty;
            txtFax.Text = string.Empty;
            txtEmail.Text = string.Empty;
            mmoRemark.Text = string.Empty;

            btnAreaCode.Focus();
        }

        private void btnAreaCode_Leave(object sender, EventArgs e)
        {
            if (AreaCode == btnAreaCode.Text.Trim())
            {
                return;
            }
            ValidateArea();
        }

        private bool ValidateArea()
        {
            var bll = new DMZS.BLL.Area();
            var row = bll.GetRecordByCode(btnAreaCode.Text.Trim());

            if (row == null)
            {
                XtraMessageBox.Show("该区域编号不存在。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                AreaID = 0;
                AreaCode = string.Empty;
                btnAreaCode.Focus();
                btnAreaCode.SelectAll();
                txtAreaName.Text = string.Empty;
                return false;
            }
            else
            {
                AreaID = Convert.ToInt32(row["ID"]);
                AreaCode = row["Code"].ToString();
                txtAreaName.Text = row["Name"].ToString();
                return true;
            }
        }

        private bool ValidateInput()
        {
            if (btnAreaCode.Text.Trim().Length == 0)
            {
                btnAreaCode.Focus();
                XtraMessageBox.Show("请输入区域编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            else
            {
                if (txtCode.Text.Trim().Length == 0)
                {
                    txtCode.Focus();
                    XtraMessageBox.Show("请输入客户编号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                else
                {
                    if (txtName.Text.Trim().Length == 0)
                    {
                        txtName.Focus();
                        XtraMessageBox.Show("请输入客户名称。", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
            }
            return true;
        }

        private void btnAreaCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DialogForm.FrmArea_dlg frm = new DialogForm.FrmArea_dlg();
            frm.ReturnList += new DialogForm.FrmArea_dlg.ReturnListEventHandler(frm_ReturnList);
            frm.ShowDialog();
        }

        void frm_ReturnList(List<System.Data.DataRow> lists)
        {
            if (lists.Count == 0) return;

            AreaID = Convert.ToInt32(lists[0]["ID"]);
            btnAreaCode.Text = lists[0]["Code"].ToString();
            txtAreaName.Text = lists[0]["Name"].ToString();
        }
    }
}
